package com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.share;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.coffeeloversupportlibrary.R;
import com.google.android.material.snackbar.Snackbar;

import static android.widget.Toast.LENGTH_SHORT;

/**
 * @author puyv
 * @createTime 2021/11/26 17:01
 * @projectName CoffeeLoverSupportLibrary
 * @className LocationServiceActivity.java
 * @description TODO  地图定位功能暂未实现
 */
public class LocationServiceActivity extends AppCompatActivity implements View.OnClickListener{

    private MapView mapView;
    private TextView click;
    private LocationClient locationClient;
    private Context context;
    private Activity currentActivity;
    private static final int REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化sdk
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.location_service);
        mapView=findViewById(R.id.map);
        context=this;
        currentActivity=this;
        BaiduMap baiduMap=mapView.getMap();
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        baiduMap.setTrafficEnabled(true);
    }

    private void create(){
        reqPermission();
        //在主线程中声明 使用全进程 通用的 application Context
        //1.创建 LocationClient对象
        locationClient=new LocationClient(getApplicationContext());
        //2.创建 LocationClientOptions 对象 配置 定位对象的相关参数
        LocationClientOption locationClientOption=new LocationClientOption();
        locationClientOption.setCoorType("gcj02");//设置坐标类型

//        国测局经纬度坐标系： "gcj02"
//        百度墨卡托坐标系："bd09"
//        百度经纬度坐标系："bd09ll"
        locationClientOption.setEnableSimulateGps(true); //激活GPS
        locationClientOption.setScanSpan(1000);//扫描件个 每秒扫一次
        locationClientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置定位模式
//        高精度定位模式：Hight_Accuracy
//        低功耗定位模式：Battery_Saving
//        仅用设备定位模式： Device_Sensors

        locationClient.setLocOption(locationClientOption);//设置定位的配置信息
        locationClient.registerLocationListener(new MyListener()); //为该定位客户端注册定位监听
        locationClient.start();

    }
    private void reqPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this.currentActivity,Manifest.permission.ACCESS_COARSE_LOCATION)
                || ActivityCompat.shouldShowRequestPermissionRationale(this.currentActivity,Manifest.permission.ACCESS_FINE_LOCATION)
                || ActivityCompat.shouldShowRequestPermissionRationale(this.currentActivity,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                || ActivityCompat.shouldShowRequestPermissionRationale(this.currentActivity,Manifest.permission.READ_PHONE_STATE)){
            ActivityCompat.requestPermissions(LocationServiceActivity.this,
                    new String[]{
                            Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_PHONE_STATE},
                    1);
        }
    }

    @Override
    public void onClick(View v) {
        create();
    }

    /**
     * @date 2021/11/15 20:37
     * @params
     * @return
     * @description TODO  和生命周期相同步的各个方法
     */
    class MyListener extends BDAbstractLocationListener {
        //BDLocation 位置信息的封装类    通过一系列方法 获取相应信息
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            double lat = bdLocation.getLatitude();
            double lon=bdLocation.getLongitude();
            Toast.makeText(context,String.valueOf(lon),LENGTH_SHORT).show();
        }
    }
}




